import { request } from '@/base/Request';
import { DeviceModel, DeviceModelName } from '@/base/model/DeviceModel';
import { DeviceModelAll } from '@/base/model/DeviceModelAll';
import { Data} from '@/base/model/Member';
import qs from 'qs';

export function getDeviceModelAll(
  params: { companyId: number }
) {
  return request.getArray(DeviceModelAll, '/api/device-model/all', { params });
}

export function getDeviceModel(params: {
  pageIndex: number,
  pageSize: number,
  sortId?: number,
  sortDirection: string,
  companyId?: [],
  id?: number,
  modelName?: string,
}) {
  let companyString = '';
  if (params.companyId !== undefined && params.companyId.length > 0) {
    const companys = params.companyId.map((x: string) => {
      return 'companyId=' + x;
    });
    companyString = companys.join('&');
  }
  delete (params.companyId);
  return request.getPage(DeviceModel, '/api/device-model?' + companyString, { params });
}

export function getDeviceModelV2(params: {
  pageIndex: number,
  pageSize: number,
  sort: number,
  id: number,
}) {
  return request.get(String, '/api/mall/fetchModelList', { params });
}
export function getDeviceModelId(params: {
  id: number
}) {
  return request.get(DeviceModel, '/api/device-model/{id}'.replace('{id}', params.id.toString()));
}

export function postDeviceModel(params: {
  companyId: number,
  internalModel:string
  isFirmware: boolean,
  isSelfMotion: boolean,
  isSoftware: boolean,
  modelIcon: string,
  modelName: string,
  configure: string,
  sortId: number,
  poster: string,
  bletype:number,
  channel:number
}) {
  return request.post(DeviceModel, '/api/device-model', qs.stringify(params));
}

export function
  putDeviceModel(params: {
    id: number,
    companyId: number,
    internalModel:string,
    isFirmware: boolean,
    isSelfMotion: boolean,
    isSoftware: boolean,
    modelIcon: string,
    modelName: string,
    sortId: number,
    isHide?: number,
    poster: string,
    configure: string,
    bletype:number,
    channel:number
  }) {
  return request.put(DeviceModel, '/api/device-model/{id}'.replace('{id}', params.id.toString()), qs.stringify(params));
}

export function deleteDeviceModel(params: {
  id: number
}) {
  return request.delete('/api/device-model/{id}'.replace('{id}', params.id.toString()));
}

export function putDeviceModelSort(params: {
  id: number[],
}) {
  return request.put(DeviceModel, '/api/device-model/sort', qs.stringify(params, { arrayFormat: 'repeat' }));
}

export function putDeviceModelName() {
  return request.put(Array, '/api/device-model/model-name');
}

export function getDeviceUsage(params: {
  startTime: any,
  endTime: any,
  deviceSn:string
}) {
  return request.getArray(Data, '/api/device/usage/count',{params});
}

export function updateMark(params: {
  deviceId: number,
  id: number
}) {
  return request.put(String,'/api/member-device/updateMark', qs.stringify(params))
}
